<template>
  <div>
    <!-- 搜索栏区域 -->
    <el-card class="SearchCard" body-style="padding:0px">
      <el-row>
        <el-col :span="2">
          <div class="SearchTitle">条件搜索栏</div>
        </el-col>
        <el-col :span="4">
          <el-row>
            <el-col :span="10">
              <div class="SearchLable">搜索关键字</div>
            </el-col>

            <el-col :span="14">
              <div class="SearchLable">
                <el-input placeholder="请输入内容" v-model="queryInfo.srm" clearable @clear="clearEvent"></el-input>
              </div>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="2">
          <div class="SearchLable">
            <el-button size="medium" type="primary" @click="getSearchList()">搜索</el-button>
          </div>
        </el-col>
        <!-- <el-col :span="2">
          <div class="SearchLable">
            <el-button
              size="medium"
              type="primary"
            >添加新药</el-button>
          </div>
        </el-col>-->
      </el-row>
    </el-card>
    <el-card class="TableCard" :body-style="{ padding: '0px' }">
      <div>
        <el-table
          :data="tableData"
          border
          :header-cell-style="{ 'text-align':'center',background: '#3F9DFD', color: '#FFF' }"
          :cell-style="{'text-align':'center'}"
          size="medium"
          stripe
          style="width: 100%;font-size:12px"
        >
          <el-table-column type="index" width="50"></el-table-column>
          <el-table-column prop="drugCode" label="药品编码" width="100"></el-table-column>
          <el-table-column prop="name" label="操作" width="120">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="mini"
                @click="addGuidelines(scope.row.drugCode,scope.row.drugName)"
              >添加</el-button>
              <el-button
                type="text"
                size="mini"
                v-if="scope.row.count!=0"
                @click="showGuidelines(scope.row.drugCode,scope.row.drugName)"
              >查看</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="drugName" label="药品名称"></el-table-column>
          <el-table-column prop="cbrand" label="商品名"></el-table-column>
          <el-table-column label="药品类型" width="80">
            <template slot-scope="scope">
              <div v-if="scope.row.nameType==8">西药</div>
              <div v-else-if="scope.row.nameType==2">草药</div>
              <div v-else-if="scope.row.nameType==6">中成药</div>
            </template>
          </el-table-column>
          <el-table-column prop="drugSpec" label="药品规格"></el-table-column>

          <el-table-column prop="compName" label="药品生产厂家"></el-table-column>
          <el-table-column prop="approvalCode" label="批准文号"></el-table-column>
        </el-table>
      </div>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[20, 50, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.total"
          background
        ></el-pagination>
      </div>
    </el-card>
    <el-dialog
      :title="'新增用药指导单'+'—'+addGuideData.drugName"
      :visible.sync="dialogVisible"
      width="35%"
      :before-close="handleClose"
    >
      <div>
        <el-input
          type="textarea"
          placeholder="请输入内容"
          v-model="addGuideData.addTextarea"
          :autosize="{ minRows: 12, maxRows: 12}"
          maxlength="80"
          show-word-limit
        ></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addInstruSheetOne()">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :title="'用药指导单'+'—'+showGuideData.drugName"
      :visible.sync="showGuideVisible"
      width="35%"
      :before-close="handleClose"
    >
      <div>
        <el-table
          :data="showGuideData.tableData"
          :header-cell-style="{ 'text-align':'center',background: '#3F9DFD', color: '#FFF' }"
          :cell-style="{'text-align':'center'}"
          stripe
          border
          height="338"
          style="width: 100%"
        >
          <el-table-column prop="vIndex" label="序号" width="70"></el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="EditGuide(scope.row.id,scope.row.vIndex, scope.row.content)"
              >查看</el-button>
              <el-button type="text" @click="open(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="content" label="内容"></el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="showGuideData.total"
            @size-change="handleSizeChange2"
            @current-change="handleCurrentChange2"
          ></el-pagination>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showGuideVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="编辑用药指导单"
      :visible.sync="EditGuideVisible"
      width="35%"
      :before-close="handleClose"
    >
      <div>
        <el-input
          placeholder="请输入内容"
          v-model="EditGuideData.vIndex"
          style="margin-bottom: 12px;"
          size="medium"
        >
          <template slot="prepend">序号</template>
        </el-input>
        <el-input
          type="textarea"
          placeholder="请输入内容"
          v-model="EditGuideData.content"
          :autosize="{ minRows: 12, maxRows: 12}"
          maxlength="80"
          show-word-limit
        ></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updataGuide()">更 新</el-button>
        <el-button @click="EditGuideVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getInstruSheetDrugList,
  addInstruSheetOne,
  getShowGuideList,
  updataGuide,
  deleteGuide
} from "@/api/rad";
export default {
  data() {
    return {
      queryInfo: {
        srm: "",
        page: 1,
        pageSize: 20
      },
      total: 0,
      tableData: [],
      dialogVisible: false,
      addGuideData: {
        drugCode: "",
        drugName: "",
        addTextarea: ""
      },
      showGuideVisible: false,
      showGuideData: {
        page: 1,
        pageSize: 6,
        drugCode: "",
        drugName: "",
        tableData: [],
        total: 0
      },
      EditGuideVisible: false,
      EditGuideData: {
        id: "",
        vIndex: "",
        content: ""
      }
    };
  },
  created: function() {
    this.getInstruSheetDrugList();
  },
  methods: {
    handleClose(done) {
      done();
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getInstruSheetDrugList();
    },

    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage;
      this.getInstruSheetDrugList();
    },
    handleSizeChange2(newSize) {
      this.showGuideData.pageSize = newSize;
      this.getShowGuideList();
    },

    handleCurrentChange2(newPage) {
      this.showGuideData.page = newPage;
      this.getShowGuideList();
    },
    async getInstruSheetDrugList() {
      let res = await this.$rad.getInstruSheetDrugList({
        srm: this.queryInfo.srm,
        page: this.queryInfo.page,
        pageSize: this.queryInfo.pageSize
      });
      (this.tableData = res.rows), (this.total = res.total);
      console.log(res.rows);
    },
    getSearchList() {
      this.getInstruSheetDrugList();
    },
    clearEvent() {
      // this.getInstruSheetDrugList();
    },
    addGuidelines(drugCode, drugName) {
      this.addGuideData.drugCode = drugCode;
      this.addGuideData.drugName = drugName;
      this.dialogVisible = true;
    },
    async addInstruSheetOne() {
      let res = await this.$rad.addInstruSheetOne({
        drugCode: this.addGuideData.drugCode,
        content: this.addGuideData.addTextarea
      });
      console.log(res);
      if (res.code == 1) {
        this.dialogVisible = false;
        this.getInstruSheetDrugList();
        this.addGuideData.addTextarea = "";
        return this.$message({
          type: "success",
          message: res.msg
        });
      } else {
        return this.$message({
          type: "error",
          message: res.msg
        });
      }
    },
    showGuidelines(drugCode, drugName) {
      this.showGuideVisible = true;
      this.showGuideData.drugCode = drugCode;
      this.showGuideData.drugName = drugName;
      this.getShowGuideList();
    },
    async getShowGuideList() {
      let res = await this.$rad.getShowGuideList({
        drugCode: this.showGuideData.drugCode,
        page: this.showGuideData.page,
        pageSize: this.showGuideData.pageSize
      });
      console.log(res);
      this.showGuideData.tableData = res.rows;
      this.showGuideData.total = res.total;
    },
    EditGuide(id, vIndex, content) {
      this.EditGuideData.id = id;
      this.EditGuideData.vIndex = vIndex;
      this.EditGuideData.content = content;
      this.EditGuideVisible = true;
    },
    async updataGuide() {
      let res = await this.$rad.updataGuide({
        id: this.EditGuideData.id,
        content: this.EditGuideData.content,
        vIndex: this.EditGuideData.vIndex
      });
      if (res.code == 1) {
        this.getShowGuideList();
        this.EditGuideVisible = false;
        return this.$message({
          type: "success",
          message: res.msg
        });
      } else {
        return this.$message({
          type: "success",
          message: res.msg
        });
      }
    },
    open(id) {
      this.$confirm("此操作将永久删除该指导单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteGuide(id);
          this.getShowGuideList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async deleteGuide(id) {
      let res = await this.$rad.deleteGuide({
        id: id
      });
      console.log(id);
      if (res.code == 1) {
        this.getShowGuideList();
        this.getInstruSheetDrugList();
        return this.$message({
          type: "success",
          message: res.msg
        });
      } else {
        return this.$message({
          type: "success",
          message: res.msg
        });
      }
    }
  }
};
</script>
<style>
.SearchCard {
  height: 50px;
  font-size: 12px;
}
.SearchTitle {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-right: 1px solid #c2c2c2;
}
.SearchLable {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.TableCard {
  margin-left: 3px;
  margin-top: 2px;
}
.block {
  text-align: center;
  margin-top: 12px;
  margin-bottom: 12px;
}
</style>